package com.turing.post.report.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.AccSysBranchDaySett;
import com.turing.post.report.util.AccSysBranchDaySettDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "report.impl.AccSysBranchDaySettDaoImpl")
@Remote
@Local
public class AccSysBranchDaySettDaoImpl implements AccSysBranchDaySettDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 插入对象
	 * 
	 * @param asbcds
	 * @throws PCPSApplicationException
	 */
	public void insert(AccSysBranchDaySett asbcds)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, asbcds);
	}

	/**
	 * 根据主键查找
	 * 
	 * @param asbcds
	 * @return
	 */
	public AccSysBranchDaySett getAccSysBranchDaySettByKey(
			AccSysBranchDaySett asbcds) {
		return _em.find(AccSysBranchDaySett.class, asbcds);
	}

	/**
	 * 删除某发卡商某日所有的销售机构日结数据
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @param saleBranchNo
	 *            销售分支机构号
	 */
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeAllAccSysBranchDaySettByTranDate(String issueCompanyNo,
			Date tranDate, String saleBranchNo) {
		Query query = _em
				.createNativeQuery("delete from AccSysBranchDaySett t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2 and t.saleBranchNo = ?3");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.setParameter(3, saleBranchNo);
		query.executeUpdate();
	}

	/**
	 * 根据发卡商号、日期、销售机构号检索小于该日期的最大日期
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @param saleBranchNo
	 *            销售分支机构号
	 * @return
	 */
	public Date getMaxTranDate(String issueCompanyNo, Date tranDate,
			String saleBranchNo) {
		Query query = _em
				.createNativeQuery("select Max(t.tranDate) from AccSysBranchDaySett t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate < ?2 and t.saleBranchNo = ?3");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.setParameter(3, saleBranchNo);
		return (Date) query.getSingleResult();
	}

	/**
	 * 根据发卡商号及日期检索所有销售机构日结数据列表
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AccSysBranchDaySett> getAllAccSysBranchDaySettByTranDate(
			String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery(
						"select t.* from AccSysBranchDaySett t where t.issueCompanyNo = ?1 and t.tranDate = ?2",
						AccSysBranchDaySett.class);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getAccSysBranchDaySettReportByCondition(
			PCPSWebInput webInput, String saleBranchNo, String startDate,
			String endDate) {
		if (webInput == null || webInput.getOperId() == null) {
			return null;
		}
		String sql = "select salebranchno,nvl(sumlastbalance,0),SaleAmount,DepositAmount,ConsumeAmount,RedemingAmount,FeeAmount,AdjustAmount,"
				+ "(nvl(sumlastbalance,0) + SaleAmount + DepositAmount - ConsumeAmount -RedemingAmount - FeeAmount - AdjustAmount) Balance"
				+ " from (select c.salebranchno, (select a.balance from AccSysBranchDaySett a"
				+ " where a.trandate =  (select max(b.trandate) from AccSysBranchDaySett b"
				+ "  where b.trandate < to_date(?1, 'yyyy-mm-dd') and a.salebranchno=b.salebranchno  ) and a.salebranchno = c.salebranchno ) "
				+ "  sumlastbalance, sum(c.SaleAmount) SaleAmount, sum(c.DepositAmount) DepositAmount, sum(c.ConsumeAmount) ConsumeAmount,"
				+ " sum(c.RedemingAmount) RedemingAmount, sum(c.FeeAmount) FeeAmount, sum(c.AdjustAmount) AdjustAmount"
				+ "   from AccSysBranchDaySett c  where c.salebranchno in ("
				+ saleBranchNo
				+ ") "
				+ "  and c.trandate <= to_date(?2, 'yyyy-mm-dd')"
				+ "   and c.trandate >= to_date(?1, 'yyyy-mm-dd')"
				+ "  group by c.SaleBranchNo  ) order by salebranchno";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		List<Object[]> list = query.getResultList();
		return list;
	}
}